package jsu.yym.tiktok.server;

import jsu.yym.tiktok.pojo.Video;
import org.springframework.stereotype.Component;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
@Component
public class VideoComparator implements Comparator<Video> {
    private String targetTag;

    public VideoComparator(String targetTag) {
        this.targetTag = targetTag;
    }

    public VideoComparator() {
    }

    @Override
    public int compare(Video video1, Video video2) {
        // 如果标签相同，按照浏览量（count）降序排序
        if(targetTag.contains(video1.getLabel())&&targetTag.contains(video2.getLabel()))
        {
            return Integer.compare(video2.getCount(), video1.getCount());
        }
        // 如果只有 video1 包含目标标签，将 video1 放在前面
        else if (targetTag.contains(video1.getLabel())) {
            return -1;
        }
        // 如果只有 video2 包含目标标签，将 video2 放在前面
        else if (targetTag.contains(video2.getLabel())) {
            return 1;
        }
        // 如果都不包含目标标签，按照浏览量（count）降序排序
        return Integer.compare(video2.getCount(), video1.getCount());
    }
    public List<Video> sortVideos(List<Video> videos) {
        return videos.stream()
                .sorted(Comparator
                        .comparingInt(Video::getCount)  // 按浏览量降序排序
                        .thenComparingInt(Video::getLikes)  // 按点赞量降序排序
                        .thenComparingInt(Video::getCollections)  // 按收藏量降序排序
                        .reversed()  // 降序排序
                )
                .collect(Collectors.toList());
    }
}
